package com.vanvalt.payhui.modules.pay.api;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;

import com.thinkgem.jeesite.modules.sys.service.OfficeService;
import com.thinkgem.jeesite.modules.sys.service.SystemService;
import com.vanvalt.payhui.modules.pay.api.business.BannerProcess;
import com.vanvalt.payhui.modules.pay.api.business.DownloadProcess;
import com.vanvalt.payhui.modules.pay.api.business.FeedbackProcess;
import com.vanvalt.payhui.modules.pay.api.business.NewsProcess;
import com.vanvalt.payhui.modules.pay.api.business.PosProcess;
import com.vanvalt.payhui.modules.pay.api.business.ProfitProcess;
import com.vanvalt.payhui.modules.pay.api.business.QuestionProcess;
import com.vanvalt.payhui.modules.pay.api.business.TokenProcess;
import com.vanvalt.payhui.modules.pay.api.business.UserProcess;
import com.vanvalt.payhui.modules.pay.api.business.UserShareProcess;
import com.vanvalt.payhui.modules.pay.api.command.impl.PageCommand;
import com.vanvalt.payhui.modules.pay.api.command.impl.RequestCommand;
import com.vanvalt.payhui.modules.pay.api.command.impl.ResponseCommand;
import com.vanvalt.payhui.modules.pay.api.util.CommandCode;
import com.vanvalt.payhui.modules.pay.api.util.ErrorCode;
import com.vanvalt.payhui.modules.pay.service.BankCardService;
import com.vanvalt.payhui.modules.pay.service.BannerService;
import com.vanvalt.payhui.modules.pay.service.DownloadService;
import com.vanvalt.payhui.modules.pay.service.FeedbackService;
import com.vanvalt.payhui.modules.pay.service.NewsService;
import com.vanvalt.payhui.modules.pay.service.PosService;
import com.vanvalt.payhui.modules.pay.service.ProfitDataService;
import com.vanvalt.payhui.modules.pay.service.ProfitService;
import com.vanvalt.payhui.modules.pay.service.QuestionService;
import com.vanvalt.payhui.modules.pay.service.UserService;
import com.vanvalt.payhui.modules.pay.service.UserShareService;
import com.vanvalt.payhui.util.constat.Constant;
import com.vanvalt.payhui.util.http.RequestBody;
import com.vanvalt.payhui.util.json.JsonConversion;
import com.vanvalt.payhui.util.token.TokenUtils;

public class RequestProcess {
	
	/**
	 * 日志.
	 */
	private static Logger					logger	= Logger.getLogger(RequestProcess.class);
	
	/**
	 * 客户端请求命令.
	 */
	private RequestCommand					requestCommand;
	
	/**
	 * 服务端响应命令.
	 */
	private ResponseCommand					responseCommand;
	
	/**
	 * 请求命令内容.
	 */
	private String							reqBody;
	/**
	 * 分页.
	 */
	private PageCommand						pageCommand;
	
	private SystemService					systemService;
	private OfficeService					officeService;
	private UserService						userService;
	private BannerService					bannerService;
	private PosService						posService;
	private DownloadService					downloadService;
	private QuestionService					questionService;
	private NewsService						newsService;
	private UserShareService				userShareService;
	private ProfitDataService				profitDataService;
	private FeedbackService					feedbackService;
	private BankCardService					bankCardService;
	private ProfitService					profitService;
	
	/**
	 * 构造方法.
	 */
	public RequestProcess() {
		this.requestCommand = new RequestCommand();
		this.responseCommand = new ResponseCommand();
	}

	/**
	 * 构造方法.
	 * 
	 * @param request
	 *            请求对象
	 */
	public RequestProcess(HttpServletRequest request) {
		responseCommand = new ResponseCommand();
		try {
			reqBody = RequestBody.getBodyRequest(request);
			requestCommand = new RequestCommand(reqBody);
		}
		catch (Exception e) {
			responseCommand.setStatus(ErrorCode.ERR_CODE_STATUS_FALSE);
			logger.error(e.getMessage(), e);
		}
	}

	/**
	 * 处理请求命令.
	 */
	public void ProcessCommand() {

		if (null == responseCommand) {
			responseCommand = new ResponseCommand();
		}

		if (null == requestCommand) {
			requestCommand = new RequestCommand();
		}
		// 用户
		UserProcess userProcess;
		// banner图
		BannerProcess bannerProcess;
		// Pos机
		PosProcess posProcess;
		// 软件下载
		DownloadProcess downloadProcess;
		// 常见问题
		QuestionProcess questionProcess;
		// 资讯
		NewsProcess newsProcess;
		// 用户分享
		UserShareProcess userShareProcess;
		// 分润记录
		ProfitProcess profitProcess;
		// 意见反馈
		FeedbackProcess feedbackProcess;
		// 七牛Token
		TokenProcess tokenProcess;
		
		try {
			
			responseCommand.setCommand(requestCommand.getCommand());
			
			final int commandKey = Integer.valueOf(requestCommand.getCommand());
			
			if(commandKey != CommandCode.CMD_SYSTEM_USER_LOGIN 
					&& commandKey != CommandCode.CMD_MEMBER_REGISTER
					&& commandKey != CommandCode.CMD_MEMBER_LOGIN
					&& commandKey != CommandCode.CMD_MEMBER_PASSWORD_RETRIEVE){ 
				
				// token验证,登录、注册跳过
				if(!checkToken()) {
					//return;			
				}
			} 
			
			switch (commandKey) {
			
					
				case CommandCode.CMD_SYSTEM_USER_LOGIN:
					// 900001	用户登录.
					userProcess = new UserProcess(userService, systemService);
					responseCommand = userProcess.login(requestCommand, responseCommand);
					break;
				case CommandCode.CMD_MEMBER_REGISTER:
					// 900003	用户注册.
					userProcess = new UserProcess(userService, systemService, officeService);
					responseCommand = userProcess.regist(requestCommand, responseCommand);
					break;
				case CommandCode.CMD_MEMBER_PASSWORD_RETRIEVE:
					// 900006	用户找回密码.
					userProcess = new UserProcess(userService, systemService);
					responseCommand = userProcess.retrievePassword(requestCommand, responseCommand);
					break;
				case CommandCode.CMD_MEMBER_PASSWORD_CHANGE:
					// 900007	用户修改密码.
					userProcess = new UserProcess(userService, systemService);
					responseCommand = userProcess.changePwd(requestCommand, responseCommand);
					break;
				case CommandCode.CMD_MEMBER_INFO_CHANGE:
					// 900008	个人中心修改.
					userProcess = new UserProcess(userService, systemService);
					responseCommand = userProcess.userInfoModify(requestCommand, responseCommand);
					break;
				case CommandCode.CMD_MEMBER_VERIFY_CODE:
					// 900009	验证码校验.
					userProcess = new UserProcess(userService, systemService);
					responseCommand = userProcess.verifyCode(requestCommand, responseCommand);
					break;
				case CommandCode.CMD_MEMBER_CENTER:
					// 900010	用户中心.
					userProcess = new UserProcess(userService, profitService, userShareService, bankCardService);
					responseCommand = userProcess.userCenter(requestCommand, responseCommand);
					break;
					
				case CommandCode.CMD_BANNER_LIST:
					// 900011	Banner图.
					bannerProcess = new BannerProcess(userService, bannerService);
					responseCommand = bannerProcess.list(pageCommand, requestCommand, responseCommand);
					break;
				case CommandCode.CMD_POS_BIG:
					// 900012	Pos机.
					posProcess = new PosProcess(userService, posService);
					responseCommand = posProcess.posList(pageCommand, requestCommand, responseCommand);
					break;
				case CommandCode.CMD_POS_SMALL:
					// 900013	手刷.
					posProcess = new PosProcess(userService, posService);
					responseCommand = posProcess.handPosList(pageCommand, requestCommand, responseCommand);
					break;
				case CommandCode.CMD_APP_DOWNLOAD:
					// 900014	软件下载.
					downloadProcess = new DownloadProcess(userService, downloadService);
					responseCommand = downloadProcess.list(pageCommand, requestCommand, responseCommand);
					break;
				case CommandCode.CMD_FAQ:
					// 900015 	常见问题.
					questionProcess = new QuestionProcess(userService, questionService);
					responseCommand = questionProcess.list(pageCommand, requestCommand, responseCommand);
					break;
				case CommandCode.CMD_NEWS:
					// 900016	资讯.
					newsProcess = new NewsProcess(userService, newsService);
					responseCommand = newsProcess.list(pageCommand, requestCommand, responseCommand);
					break;
				case CommandCode.CMD_USER_SHARE:
					// 900017	用户分享记录.
					userShareProcess = new UserShareProcess(userService, userShareService);
					responseCommand = userShareProcess.userShareList(pageCommand, requestCommand, responseCommand);	
					break;
				case CommandCode.CMD_USER_PROFIT:
					// 900018	用户分润记录.
					profitProcess = new ProfitProcess(userService, profitDataService);
					responseCommand = profitProcess.list(pageCommand, requestCommand, responseCommand);	
					break;
				case CommandCode.CMD_USER_FEEDBACK:
					// 900019	意见反馈.
					feedbackProcess = new FeedbackProcess(userService, feedbackService);
					responseCommand = feedbackProcess.feedback(requestCommand, responseCommand);	
					break;
				case CommandCode.CMD_QINIU_TOKEN:
					// 900020	七牛Token.
					tokenProcess = new TokenProcess(userService);
					responseCommand = tokenProcess.generateToken(requestCommand, responseCommand);	
					break;
					
				default:
					responseCommand.setCode(ErrorCode.ERR_CODE_UNKNOWN_COMMAND);
					responseCommand.setStatus(ErrorCode.ERR_CODE_STATUS_FALSE);
					break;
			}
		} catch (Exception e) {

			String reqBody = requestCommand.getCommand() + " " + requestCommand.getObject();

			Map<String, String> map = new HashMap<String, String>();
			map.put("requetBody", reqBody);
			map.put("class", e.getClass().toString());
			map.put("message", e.getMessage());
			responseCommand.setObject(JsonConversion.writeMapJSON(map));

			responseCommand.setCode(ErrorCode.ERR_CODE_EXCEPTION);
			responseCommand.setStatus(ErrorCode.ERR_CODE_STATUS_FALSE);
			logger.error(reqBody + " " + e.getMessage(), e);
		}

	}

	/**
	 * 校验令牌合法
	 */
	private boolean checkToken() {
		if (StringUtils.isEmpty(requestCommand.getToken())) {
			responseCommand.setStatus(ErrorCode.ERR_CODE_STATUS_FALSE);
			responseCommand.setCode(ErrorCode.ERR_CODE_NOT_AUTHORIZE_LOGIN_FAILURE);
			return ErrorCode.ERR_CODE_STATUS_FALSE;
		}else {
			// [0]是密码，[1]是token,[2]是nextToken
			String token[] = StringUtils.split(requestCommand.getToken(), Constant.SPLIT_FILE);
			if (null == token || token.length != 3) {
				responseCommand.setStatus(ErrorCode.ERR_CODE_STATUS_FALSE);
				responseCommand.setCode(ErrorCode.ERR_CODE_NOT_AUTHORIZE_LOGIN_FAILURE);
				return ErrorCode.ERR_CODE_STATUS_FALSE;
			}
			if (!TokenUtils.validToken(token[1], token[0]) 
					&& !TokenUtils.validToken(token[2], token[0])) {
				// 登录失败，未授权，token非法
				responseCommand.setStatus(ErrorCode.ERR_CODE_STATUS_FALSE);
				responseCommand.setCode(ErrorCode.ERR_CODE_NOT_AUTHORIZE_LOGIN_FAILURE);
				return ErrorCode.ERR_CODE_STATUS_FALSE;
			}
		}
		
		return ErrorCode.ERR_CODE_STATUS_TRUE;
	}

	public RequestCommand getRequestCommand() {
		return requestCommand;
	}

	public void setRequestCommand(RequestCommand requestCommand) {
		this.requestCommand = requestCommand;
	}

	public ResponseCommand getResponseCommand() {
		return responseCommand;
	}

	public void setResponseCommand(ResponseCommand responseCommand) {
		this.responseCommand = responseCommand;
	}

	public String getReqBody() {
		return reqBody;
	}

	public void setReqBody(String reqBody) {
		this.reqBody = reqBody;
	}

	public PageCommand getPageCommand() {
		return pageCommand;
	}

	public void setPageCommand(PageCommand pageCommand) {
		this.pageCommand = pageCommand;
	}

	public BannerService getBannerService() {
		return bannerService;
	}

	public void setBannerService(BannerService bannerService) {
		this.bannerService = bannerService;
	}

	public SystemService getSystemService() {
		return systemService;
	}

	public void setSystemService(SystemService systemService) {
		this.systemService = systemService;
	}

	public UserService getUserService() {
		return userService;
	}

	public void setUserService(UserService userService) {
		this.userService = userService;
	}

	public PosService getPosService() {
		return posService;
	}

	public void setPosService(PosService posService) {
		this.posService = posService;
	}

	public DownloadService getDownloadService() {
		return downloadService;
	}

	public void setDownloadService(DownloadService downloadService) {
		this.downloadService = downloadService;
	}

	public QuestionService getQuestionService() {
		return questionService;
	}

	public void setQuestionService(QuestionService questionService) {
		this.questionService = questionService;
	}

	public NewsService getNewsService() {
		return newsService;
	}

	public void setNewsService(NewsService newsService) {
		this.newsService = newsService;
	}

	public UserShareService getUserShareService() {
		return userShareService;
	}

	public void setUserShareService(UserShareService userShareService) {
		this.userShareService = userShareService;
	}

	public ProfitDataService getProfitDataService() {
		return profitDataService;
	}

	public void setProfitDataService(ProfitDataService profitDataService) {
		this.profitDataService = profitDataService;
	}

	public OfficeService getOfficeService() {
		return officeService;
	}

	public void setOfficeService(OfficeService officeService) {
		this.officeService = officeService;
	}

	public FeedbackService getFeedbackService() {
		return feedbackService;
	}

	public void setFeedbackService(FeedbackService feedbackService) {
		this.feedbackService = feedbackService;
	}
	public BankCardService getBankCardService() {
		return bankCardService;
	}

	public void setBankCardService(BankCardService bankCardService) {
		this.bankCardService = bankCardService;
	}

	public ProfitService getProfitService() {
		return profitService;
	}

	public void setProfitService(ProfitService profitService) {
		this.profitService = profitService;
	}
	
}
